package cn.zz.cameralibrary.view.scrawl;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/**
 * author: zhu on 2017/7/18 17:30
 * email: mackkilled@gmail.com
 */

public class DrawingImageView extends LinearLayout {
    private float mRatio = 0.0F;

    public DrawingImageView(Context context) {
        super(context);
    }

    public DrawingImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setRatio(float ratio) {
        this.mRatio = ratio;
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(this.mRatio != 0.0F) {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);
            int newHeight;
            int newWidth;
            if((float)width / this.mRatio < (float)height) {
                newWidth = width;
                newHeight = Math.round((float)width / this.mRatio);
            } else {
                newHeight = height;
                newWidth = Math.round((float)height * this.mRatio);
            }

            int newWidthSpec = MeasureSpec.makeMeasureSpec(newWidth, MeasureSpec.EXACTLY);
            int newHeightSpec = MeasureSpec.makeMeasureSpec(newHeight, MeasureSpec.EXACTLY);
            super.onMeasure(newWidthSpec, newHeightSpec);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }

    }
}